package tq.com.tqteacher.activity.newPublish;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQLetShareModel;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.view.TQHShowView;
import tq.com.tqteacher.view.TQHeader3ViewHold;
import tq.com.tqteacher.view.TQVShowView;
import tq.com.tqteacher.view.viewhold.TQPublishSaveButtonViewHold;

public class TQLetShareResultActivity extends BaseActivity {
    private TQHeader5View mHeader5View;

    private TQHShowView mTitle;
    private TQHShowView mGrade;
    private TextView mDesc;
    private TQHShowView mShareCount;
    private TQPublishSaveButtonViewHold mTQPublishSaveButtonViewHold;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_letshareresult_layout);
        setStatusBar();

        mHeader5View = findViewById(R.id.title);
        mHeader5View.getTitleView().setText("请ta分享");
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQLetShareResultActivity.this.finish();
            }
        });

        mShareCount = findViewById(R.id.share_count);
        mTitle = findViewById(R.id.title0);
        mGrade = findViewById(R.id.grad);
        mDesc = findViewById(R.id.desc);

        if(mTQPublishSaveButtonViewHold == null){
            mTQPublishSaveButtonViewHold = new TQPublishSaveButtonViewHold(findViewById(R.id.bottom));
            mTQPublishSaveButtonViewHold.setSaveClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            mTQPublishSaveButtonViewHold.setPublishClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onPublish();
                }
            });
        }

        findViewById(R.id.modify).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoModify();
            }
        });
        findViewById(R.id.content_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoContent();
            }
        });

        if(TQPublishType.getSharePublishItem().getNeedloadRemote()){
//            getDetail();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        bindData();
    }

    private void bindData(){
        TQLetShareModel lModel = null;
        if(TQPublishType.getSharePublishItem() != null &&
                TQPublishType.getSharePublishItem() instanceof TQLetShareModel){
            lModel = (TQLetShareModel)TQPublishType.getSharePublishItem();
            lModel.setModify(true);
        }
        if(lModel != null){
            mTitle.setText(lModel.getName());
            mGrade.setText(TQSystemDefine.gradeName(lModel.getGrade()));

            mDesc.setText(lModel.getDesc());
            mShareCount.setText(String.valueOf(lModel.getShareCount()));
        }
        if(TQPublishType.getSharePublishItem().isPublish()){
            mTQPublishSaveButtonViewHold.setAlreadyPublish();
        }
    }

    private void gotoModify(){
        Intent lIntent = new Intent(this, TQLetShareActivity.class);
        startActivity(lIntent);
    }

    private void gotoContent(){
        Intent lIntent = new Intent(this, TQLetShareContent.class);
        startActivity(lIntent);
    }

    private void getDetail(){
//        TQNetInterface.getMatrialDetail(TQPublishType.getSharePublishItem().getSeq_id(), new INetCallback() {
//            @Override
//            public void result(TQNetResult pNetResult) {
//                if(!pNetResult.isSuccess()){
//                    AlertDialogHelper.show(TQLetShareResultActivity.this, pNetResult.getErrMsg());
//                    return;
//                }
//
//                if(pNetResult.getData() == null) return;
//                try {
//                    JSONObject lObject = pNetResult.getData().getJSONObject("material");
//                    JSONObject lObject1 = lObject.getJSONObject("material");
//                    TQPublishType.getSharePublishItem().fromJson(lObject1);
//
//                    bindData();
//
//                    JSONArray lAttach = lObject.getJSONArray("attachment");
//                    TQPublishType.getSharePublishItem().refreshAttach(lAttach);
//                }catch (JSONException pE){
//                    pE.printStackTrace();
//                }
//            }
//        });

        TQPublishType.getSharePublishItem().getDetailFromServer(this, new TQNewPublish.INetFinishListener() {
            @Override
            public void finish() {
                bindData();
            }
        });
    }

    private void onPublish(){
        if(TQPublishType.getTQOpenFrom() == TQPublishType.TQOpenFrom.optimize){
            AlertDialogHelper.show(this, "已同步更新");
        }else {

            TQPublishType.getSharePublishItem().onPublish(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        AlertDialogHelper.show(TQLetShareResultActivity.this, pNetResult.getErrMsg());
                        return;
                    }

                    TQPublishType.getSharePublishItem().setState(TQSystemDefine.PUBLISH_STATE_Publish);
                    mTQPublishSaveButtonViewHold.setAlreadyPublish();

                    TQEventBus.post(new TQEventBus.TQEventMessage(TQEventBus.TQEventMessage.Opertor.modify, null));

                    TQLetShareResultActivity.this.finish();
                }
            });
        }

    }
}
